package model

import (
	"gorm.io/gorm"
	"shop-api/consts"
	"shop-api/databases/mysql"
)

type GoodType struct {
	Base
	Name     string `json:"name" gorm:"type:varchar(255);not null;comment:分类名"`
	PID      uint   `json:"pid" gorm:"index;not null;comment:父级ID"`
	Icon     string `json:"icon" gorm:"type:string;comment:Icon"`
	Lv       uint   `json:"lv" gorm:"type:tinyint(1);comment:分类等级"`
	Position uint   `json:"position" gorm:"type:tinyint(1);comment:分类展示位置"`
}

func (GoodType) GetAll() ([]*GoodType, error) {
	var infos []*GoodType
	err := mysql.WithClient(func(db *gorm.DB) error {
		return db.Find(&infos).Error
	})
	return infos, err
}

func (GoodType) GetAllByPosition(position uint) ([]*GoodType, error) {
	var infos []*GoodType
	err := mysql.WithClient(func(db *gorm.DB) error {
		return db.Where("lv = ? AND position = ?", consts.GoodTypeLv1, position).Find(&infos).Error
	})
	return infos, err
}
